package cn.pzhu.pserson.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.pzhu.pserson.dto.PeoplenumDTO;
import cn.pzhu.pserson.model.Peoplenum;
import cn.pzhu.pserson.util.ResponseUtil;
import cn.pzhu.pserson.service.IPeoplenumService;
import cn.pzhu.pserson.dto.LimitShowDTO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("/peoplenum")
public class PeoplenumController {

    @Autowired
    private IPeoplenumService peoplenumService;

    @CrossOrigin
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> peoplenumList = peoplenumService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageNum());
        long count = peoplenumService.count();

        result.put("rows", peoplenumList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @CrossOrigin
    @RequestMapping(value = "/wgadd", method = RequestMethod.POST, produces = {"text/plain;charset=utf-8","text/html;charset=utf-8"}             )
    @ResponseBody
    public ResponseEntity<String> saveCode(
            @Validated
                    LimitShowDTO limitShowDTO, BindingResult errors, HttpServletResponse response, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();

        List<Peoplenum> peoplenumList = peoplenumService.loadModels(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageNum());

        Peoplenum peoplenum = peoplenumList.get(0);

        Integer wg = peoplenum.getWgNum();
        peoplenum.setWgNum(wg + 1);

        peoplenumService.update(peoplenum);

        return ResponseUtil.getResEntityForGet(true);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<String> show() {
        Map<String, Object> peoplenumModel = peoplenumService.findMap();

        return ResponseUtil.getResEntityForGetAndJson(peoplenumModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        PeoplenumDTO peoplenumDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Peoplenum peoplenumModel = peoplenumDTO.toModel();

        return ResponseUtil.getResEntityForPPP(peoplenumService.save(peoplenumModel));
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
        @Validated
        PeoplenumDTO peoplenumDTO, BindingResult errors) {

        Peoplenum peoplenumModel = peoplenumDTO.toModel();
        return ResponseUtil.getResEntityForPPP(peoplenumService.update(peoplenumModel));
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory() {
        return ResponseUtil.getResEntityForDel(peoplenumService.delete());
    }

}
